package com.xinzhi.savingpot.interceptor;


import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.xinzhi.savingpot.rminfo.entity.Recordinfo;
import com.xinzhi.savingpot.rminfo.mapper.ILimitAllMapper;
import com.xinzhi.savingpot.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BanInterceptor implements HandlerInterceptor {
    @Autowired
    private ILimitAllMapper iLimitAllMapper;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 根据ban判断用户的账号状态（0为正常，1为封禁）
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从请求头中获取token
        String token = request.getHeader("token");
        //执行认证
        if (token == null) {
            throw new RuntimeException("尚未登陆!");
        }
        //获取token中的主体部分
        String userid;
        try {
            //从解密的token中获取主体部分
            userid = JWT.decode(token).getAudience().get(0);
        } catch (JWTDecodeException j) {
            throw new JWTDecodeException("token异常!请重新登陆");
        }
        if (!redisUtil.hasKey("endtime-"+userid)){
            Object endtime = redisUtil.get("Endtime");
            String endtime1 = endtime.toString();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date endtime2 = null;
            try {
                endtime2 = format.parse(endtime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date now;
            Calendar c = Calendar.getInstance();
            now = new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
            if (endtime2.after(now)){
                return true;
            }else {
                throw new RuntimeException("您的账号已被封禁,暂无该权限");
            }
        }
        List<Recordinfo> limitAll = iLimitAllMapper.getLimitAll(Integer.parseInt(userid));
        for (Recordinfo re:limitAll) {
            Date endtime = re.getEndtime();
            Date now;
            Calendar c = Calendar.getInstance();
            now = new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
            if (endtime.after(now)) {
                return true;
            } else {
                System.out.println("拦截到了");
                throw new RuntimeException("您的账号已被封禁,暂无该权限");
            }
        }
        System.out.println("没有拦截");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
